class RepositoriesController < ApplicationController

	include SendFile

	layout 'standard'

	before_filter :login_required

	def index
		@repositories = Repository.find(:all, :order => "id desc")
	end

	def create
		@repository = Repository.new(params[:repository])

		if @repository.save
			redirect_to repositories_path
		else
			render :action => :new
		end
	end

	def do_load
		@repository = Repository.new(params[:repository])

		if @repository.save
			redirect_to loading_repository_path(@repository)
		else
			render :action => :load
		end
	end

	def loading
		@repository = Repository.find(params[:id])
	end

	def wait_load
		@repository = Repository.find(params[:id])

		load_log = []
		File.foreach("/tmp/load_log_#{@repository.name}") {|l| load_log.push(l) }

		unless %x(ps auxw | grep "svnadmin load" | grep -v "grep").blank?
			render :text => load_log.reverse.join("<br/>")
		else
			render :text => "load_complete"
		end
	end

	def backup
		@repository = Repository.find(params[:id])

		backup = @repository.backup

		xsend_file(backup,
			:disposition => 'inline',
			:encoding => 'utf8',
			:type => 'application/octet-stream',
			:filename => URI.encode("#{@repository.name}.dump"))
	end

	def destroy
		# render :nothing => true

		#
		# BLOCKED!
		#
		@repository = Repository.find(params[:id])
		@repository.destroy
		redirect_to repositories_path
	end

end
